﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Threading;
using System.Collections.Generic;

namespace WindowsPhoneApplication1.Connectors
{
    public class PicturesConnector : Connector
    {

        public static void GetNewestPictures()
        {
            ThreadPool.QueueUserWorkItem(q =>
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ApiUrl + "pictures/all?limit=20");
                req.Method = "GET";
                req.Headers["X-Blip-API"] = "0.02";
                req.Accept = "application/json";

                req.BeginGetResponse(new AsyncCallback(
                    delegate(IAsyncResult asynchronousResult)
                    {
                        HttpStatusCode statusCode;
                        string resultString = "";

                        if (!req.HaveResponse)
                        {
                            throw new WebException();
                        }

                        try
                        {
                            using (HttpWebResponse resp = (HttpWebResponse)((HttpWebRequest)asynchronousResult.AsyncState).EndGetResponse(asynchronousResult))
                            {
                                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                                {
                                    resultString = sr.ReadToEnd();
                                    statusCode = resp.StatusCode;
                                    sr.ReadToEnd();
                                    sr.Close();
                                }
                            }
                        }
                        catch
                        {
                            statusCode = HttpStatusCode.GatewayTimeout;
                        }

                        if (statusCode == HttpStatusCode.OK)
                        {
                            try
                            {
                                List<WebServiceDataSources.Picture> pictures = new List<WebServiceDataSources.Picture>();
                                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(resultString)))
                                {
                                    var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(pictures.GetType());
                                    pictures = ser.ReadObject(ms) as List<WebServiceDataSources.Picture>;

                                    if (Snlp != null)
                                    {
                                        Snlp.UpdateNewestPhotos(pictures);
                                    }
                                    else if (Alpp != null)
                                        Alpp.UpdateNewestPhotos(pictures);
                                }
                            }
                            catch (Exception e)
                            {
                                e.ToString();
                                GetNewestPictures();
                            }
                        }
                    }), req);
            });
        }
    }
}
